package com.herui.iot.utils;

import android.os.Environment;


import com.herui.sdyu_lib.utils.FileUtils;
import com.herui.sdyu_lib.utils.StringUtils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;



/**
 * Created by amanda.shan on 2016/6/22.
 */
public class StorageUtils {

    private static Properties properties = new Properties();

    public static void init() throws IOException {
        String externalCacheDir = FileUtils.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        File file = new File(externalCacheDir, "watchkv.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        properties.load(new FileReader(file));
    }

    public static void store() {
        String externalCacheDir = FileUtils.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        File file = new File(externalCacheDir, "watchkv.txt");
        System.out.println(file.getPath());
        try {
            properties.store(new FileWriter(file), "修改");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param param
     */
    public static void saveSharedPreference(Map<String, Object> param) {//存储多个

        if (param == null || param.size() == 0)
            return;

        for (String key : param.keySet()) {
            Object value = param.get(key);
            properties.setProperty(key, value.toString());
        }
        store();
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, Integer value) {//存储单个

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, Boolean value) {

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }

    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, String value) {

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }


    /**
     * 存储数据到 SharedPreference
     *
     * @param key
     * @param value
     */
    public static void saveSharedPreference(String key, Long value) {

        if (key == null || value == null)
            return;

        Map<String, Object> param = new HashMap<String, Object>();
        param.put(key, value);
        saveSharedPreference(param);
    }

    public static String getStringSP(String key) {//得到存储，无放回空
        if (key != null) {
            return properties.getProperty(key, "");
        }
        return null;
    }

    public static Boolean getBooleanSP(String key) {//得到Boolean，前提是你存储的也是Boolean
        if (key != null) {
            String stringSP = getStringSP(key);
            if (StringUtils.isEmpty(stringSP)) {
                return false;
            }
            return Boolean.valueOf(stringSP);
        }
        return null;
    }

    public static Integer getIntegerSP(String key) {
        if (key != null) {
            String stringSP = getStringSP(key);
            if (StringUtils.isEmpty(stringSP)) {
                return 0;
            }
            return Integer.valueOf(stringSP);
        }
        return null;
    }

    public static Long getLongSP(String key) {
        if (key != null) {
            String stringSP = getStringSP(key);
            if (StringUtils.isEmpty(stringSP)) {
                return 0l;
            }
            return Long.valueOf(stringSP);
        }
        return null;
    }

    public static void remove(String key) {
        if (key != null) {
            properties.remove(key);
            store();
        }
    }

}
